package com.nhim.cms.admin.shared;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.nhim.cms.admin.client.style.Styles;

public class MessageManager {

	public MessageManager() {
		createErrorPopup();
	}

	/**
	 * The popup used to display errors to the user.
	 */
	private final PopupPanel errorPopup = new PopupPanel(false, true);

	/**
	 * The label inside the error popup.
	 */
	private final HTML errorPopupMessage = new HTML();

	/**
	 * Show the error popup.
	 * 
	 * @param errorMessage
	 *            the error message
	 */
	public void showErrorPopup(String errorMessage) {
		errorPopupMessage.setText(errorMessage);
		errorPopup.center();
	}
	
	public void showErrorPopup(SafeHtml safeHtml)
	{
		errorPopupMessage.setHTML(safeHtml);
		errorPopup.center();
	}
	public void showErrorPopup(List<String> values)
	{
		errorPopupMessage.setHTML(getErrorMessages(values));
		errorPopup.center();
	}

	/**
	 * Create the error message popup.
	 */
	private void createErrorPopup() {
		errorPopup.setGlassEnabled(true);
		errorPopup.setStyleName(Styles.common().popupMessage());
		errorPopupMessage.addStyleName(Styles.common()
				.errorPopupMessage());

		Button closeButton = new Button("Dismiss", new ClickHandler() {
			public void onClick(ClickEvent event) {
				errorPopup.hide();
			}
		});
		closeButton.addStyleName(Styles.controPanel().btn());
		// Organize the widgets in the popup.
		VerticalPanel layout = new VerticalPanel();
		layout.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		layout.add(errorPopupMessage);
		layout.add(closeButton);
		errorPopup.setWidget(layout);
	}

	public  SafeHtml getErrorMessages(List<String> validation) {

		SafeHtmlBuilder builder = new SafeHtmlBuilder();
		for (String item : validation) {
			builder.appendEscaped(item).appendHtmlConstant("<br/>");

		}
		return builder.toSafeHtml();

	}

}
